<template>
  <div class="login-account">
    <el-form ref="formRef" label-width="50" :rules="rules" :model="account">
      <el-form-item label="账号" prop="name">
        <el-input v-model="account.name" />
      </el-form-item>
      <el-form-item label="密码" prop="passWord">
        <el-input v-model="account.passWord" />
      </el-form-item>
    </el-form>
  </div>
</template>
<script lang="ts">
// 账户登录选项
import { defineComponent, reactive, ref } from 'vue'
import { ElForm } from 'element-plus'
export default defineComponent({
  components: {},
  setup() {
    const account = reactive({
      name: '',
      passWord: ''
    })
    // 配置验证规则
    const rules = {
      // 用户名的规则
      name: [
        // 必输项，在失焦时验证
        {
          required: true,
          message: '用户名不能为空',
          trigger: 'blur'
        },
        // 正则验证
        {
          pattern: /^[a-z0-9]{5,10}$/,
          message: '用户名应为字母或者数字，5到10个字符',
          trigger: 'blur'
        }
      ],
      passWord: [
        {
          required: true,
          message: '密码不能为空',
          trigger: 'blur'
        },
        {
          pattern: /^[a-z0-9]{3,}$/,
          message: '密码应为字母或者数字，最少3个字符',
          trigger: 'blur'
        }
      ]
    }
    const formRef = ref<InstanceType<typeof ElForm>>()
    const loginAction = () => {
      formRef.value?.validate((valid) => {
        if (valid) {
          console.log('执行登录操作')
        }
      })
    }
    return {
      account,
      rules,
      formRef,
      loginAction
    }
  }
})
</script>
<style lang="scss" scoped></style>
